clear all
use  "../../data/sue_sample.dta", clear
set linesize 255
local beg_year 1996
local end_year 2017

gen yr = year(rdq1)
gen qtr = quarter(rdq1)
gen day = day(rdq1)

* Decile rank of SUE and absolute SUE
local filter  yr>=`beg_year' & yr<=`end_year' & ba_state != "PR" & rdq1< end_date & rdq1>= beg_date & type != 3.2 & no_hqadd_ctychg<=2 & ((county_group==1) |(county_group>1 &  (rdq1-beg_date)/365>=3 )) & ba_state != "AK" & ba_state != "HI" & (exchcd ==1 | exchcd ==2 | exchcd ==3) & !missing(infc) & !missing(size) & !missing(bm) & !missing(rdq1) & !missing(sue_2)
capture drop abs_sue_2
gen abs_sue_2 = abs(sue_2)
foreach yvar in  sue_2 abs_sue_2 outdegree eigenvector infc{
    capture drop `yavr'_rknew
	bysort yr qtr: egen `yvar'_rknew = xtile(`yvar') if `filter' , nq(10)
}
label variable sue_2_rknew "SUE"
label variable outdegree_rknew "DC"
label variable eigenvector_rknew "EC"
label variable infc_rknew "IC"
label variable abs_sue_2_rknew "|SUE|"

* Winsorize ep2 and evol2
foreach yvar in  ep2 evol2 {
    capture drop `yvar'_winsor
    bysort yr qtr: egen `yvar'_winsor = winsor(`yvar'), p(0.01 0.99)
}

* Decile rank of nrdq
bysrot yr qtr: egen nrdq_rk2 = xtile(nrdq), nq(10)

gen logme = log(size)
gen logbm = log(bm)
replace xad = 0 if missing(xad)
gen xad_log = log(1+xad)

local sue sue_2_rknew
*local sue sue_2_rank_sandy
local retTag dgtw_r
local retList abcret01 abcret295
** Return list
local ret
foreach yvar in `retList'{
local ret `ret' `yvar'_`retTag'
} 
label variable abcret01_dgtw_r "CAR[0 1]"
label variable abcret295_dgtw_r "CAR[2 61]"

* Control variables
local cxn outdegree_rknew eigenvector_rknew infc_rknew
*local cxn CA_rknew AC_rknew
local ctrlList logme logbm idvol ior urban ep2_winsor evol2_winsor nrdq_rk2 rptlag
local attnList log_xad sp500 retail pop_density sind_p_ctex
local locbiasList mean_age_ct retire_ratio_ct median_income_ctex mean_edu movedin_ctex 
local dummyList i.yr i.qtr i.day i.ff10 

* Control variables interacted with SUE
qui{
  ** Regular Interactions
  local ctrlInteract
  * create interactions
  foreach yvar in `ctrlList'{
    local ctrlInteract `ctrlInteract' c.`yvar'#c.`sue' `yvar'
  }
  ** Attention interactions
  local attnInteract
  foreach yvar in `attnList'{
    local attnInteract `attnInteract' c.`yvar'#c.`sue' `yvar'
  }
  display "`attnInteract'"

  ** LocalBias interactions
  local locbiasInteract
  foreach yvar in `locbiasList'{
    local locbiasInteract `locbiasInteract' c.`yvar'#c.`sue' `yvar'
  }
  display "`locbiasInteract'"

  ** Dummy Interaction
  local dummyInteract
  foreach yvar in `dummyList'{
    local dummyInteract `dummyInteract' `yvar'#c.`sue' `yvar'
  }
  display "`dummy'"
}


* Sample filter; 
local beg_year 1996
local end_year 2017
* 1. Restrict sample period
* 2. Exchange NYSE NASDAQ and AMEX
* 3. CONUS only
* 4. Exclude the first 3 years of obs after HQ change; 
local filter  yr>=`beg_year' & yr<=`end_year' & ba_state != "PR" & rdq1< end_date & rdq1>= beg_date & type != 3.2 & no_hqadd_ctychg<=2 & ((county_group==1) |(county_group>1 &  (rdq1-beg_date)/365>=3 )) & ba_state != "AK" & ba_state != "HI" & (exchcd ==1 | exchcd ==2 | exchcd ==3) & !missing(outdegree_rknew) & !missing(size) & !missing(bm) & !missing(rdq1) & !missing(sue_2)



*** Table 1
*** Summary stats
  label variable outdegree_scaled 
  winsor2 xad if `filter', suffix(_w) cuts(1, 100)
  winsor2 bm if `filter', suffix(_winsor) cuts(1, 100)
  * Normalize CEN
  foreach yvar in outdegree eigenvector infc{
    qui: sum `yvar' if `filter', d
    local pmax = r(max)
    qui: gen `yvar'_scaled = `yvar'/`pmax'*100
  }
  * Winsorize volumes
  foreach yvar in vom_ab01_pre_r vom_ab95_pre_r{
    winsor2 `yvar' if `filter', suffix(w) cuts(1, 100)
  }
	noisily tabstat outdegree_scaled eigenvector_scaled infc_scaled sue_2 abcret01_dgtw abcret295_dgtw_r vom_ab01_pre_rw vom_ab95_pre_rw ///
		size bm_winsor idvol ep2_winsor evol2_winsor rptlag ior xad_w nrdq sind_p_ctex ///
		xad_w mean_age_ct retire_ratio_ct mean_edu median_income_ctex pop_density movedin_ctex ///
		if `filter',  statistics(n mean median sd sk p10 p25 p75 p90) columns(statistics) 

	noisily corr outdegree_scaled eigenvector_scaled infc_scaled sue_2 abcret01_dgtw abcret295_dgtw_r vom_ab01_pre_rw vom_ab95_pre_rw ///
		size bm_winsor idvol ep2_winsor evol2_winsor rptlag ior xad_w nrdq sind_p_ctex ///
		xad_w mean_age_ct retire_ratio_ct mean_edu median_income_ctex pop_density movedin_ctex ///
		if `filter'

*** Table 2

  label variable abcret01_dgtw "CAR[0, 1]"
  label variable abcret240_pre_r "CAR[2, 40]"
  label variable abcret295_dgtw_r "CAR[2, 61*]"

  local ret abcret01_dgtw abcret240_pre_r abcret295_dgtw_r 
  local s sue_2_rknew
  label variable `s' "SUE"
  local cxn outdegree_rknew eigenvector_rknew infc_rknew

  eststo clear
  foreach y in `ret'{
      foreach x in `cxn'{
      capture drop CEN
      gen CEN = `x'
      local KeepVars c.CEN#c.`s' CEN `s' 	    
      eststo: quietly reghdfe `y'  c.CEN#c.`s' c.CEN `s' `ctrlInteract' `attnInteract' `dummyInteract' `locbiasInteract'   if `filter', noabsorb vce(cluster gvkey rdq1)  
      }
  }
  noisily:    esttab, ar2  star(* 0.1 ** 0.05 *** 0.01)  keep (`KeepVars') nonote label


*** Table 3: Volatility Persistence
  local cxn outdegree_rknew eigenvector_rknew infc_rknew
  eststo clear
  foreach y in d_ret0{  
      foreach x in `cxn'{
          local s abs_sue_2_rknew
          label variable `s' "|SUE|"
          capture drop CEN
          gen CEN=`x'
          
          local KeepVars  `s' CEN
          eststo: quietly reghdfe `y'   CEN `s' `ctrlList' `attnList'  `dummyList' `locbiasList' if `filter', noabsorb vce(cluster gvkey rdq1) 
      }
  }
  noisily:    esttab, star(* 0.1 ** 0.05 *** 0.01)  keep (`KeepVars') compress nonote transform(@*100) ar2  label b(3)  


*** Table 4: Abnormal Volumes and Persistence

  local cxn outdegree_rknew eigenvector_rknew infc_rknew
  local vomList vom_ab01_pre_rw vom_ab95_pre_rw d_vom_ab_pre_5

  lable variable vom_ab01_pre_rw "LNVOL[0, 1]"
  label variable vom_ab295_pre_r "LNVOL[2, 61]"
  label variable d_vom_ab_pre_5 "d_{VOL}"

  eststo clear
  foreach y in `vomList'{  
      foreach x in `cxn'{
          local s abs_sue_2_rknew
          label variable `s' "|SUE|"
          capture drop CEN
          gen CEN=`x'
          
          local KeepVars  `s' CEN
          eststo: quietly reghdfe `y'   CEN `s' `ctrlList' `attnList'  `dummyList' `locbiasList' if `filter', absorb(state) vce(cluster gvkey rdq1) 
      }
  }
  noisily:    esttab, star(* 0.1 ** 0.05 *** 0.01)  keep (`KeepVars') compress nonote transform(@*100) ar2  label b(3) 

*** Table 5: StockTwits Messages
* dirmenas is prefix for new messages
* imen_sp is the prefix for replies
  local zlist dirmenas imen_sp
       
  foreach stwit in `zlist'{
    
    * Abnormal New Messages and Replies
    local tag 
    capture drop abstwit_295
    gen abstwit_295 = ab_`stwit'_295`tag'
    capture drop abstwit_295_w
    winsor2 abstwit_295 if `filter', suffix(_w) cuts(1,99) 

    capture drop abstwit_01
    gen abstwit_01 = ab_`stwit'_01`tag'
    capture drop abstwit_01_w
    winsor2 abstwit_01 if `filter', suffix(_w) cuts(1,99) 
    
    * |SUE| rank for the StockTwits sample
    capture drop SUE
    astile SUE = abs_sue_2 if `filter' & !missing(abstwit_01_w) & !missing(SCEN), by(yr qtr) nq(10)
    label variable SUE "|SUE|"

    
    local attn abstwit_01_w abstwit_295_w
    local KeepVars SUE CEN
    eststo clear
    foreach y in `attn'{
      foreach x in outdegree eigenvector infc{ 

      capture drop CEN
      astile CEN = `x' if `filter' & !missing(`y'), by(yr qtr) nq(10)

      eststo: quietly reghdfe `y' SUE CEN SCEN `ctrlList' `attnList' `locbiasList' `dummyList' if `filter',   noabsorb vce(cluster gvkey rdq1) 
      
      }
    }

     noisily:    esttab, ar2 star(* 0.1 ** 0.05 *** 0.01)  keep (`KeepVars')  transform(@*100) label b(2) nonote
  }    

*** Table 6: StockTwits Disagreement

  local zlist amen_ppos_std_sp imen_ppos_std_sp
       
  foreach stwit in `zlist'{
    
    local tag 
    capture drop abstwit_295
    gen abstwit_295 = `stwit'_295`tag'
    capture drop abstwit_295_w
    winsor2 abstwit_295 if `filter', suffix(_w) cuts(1,99) 

    capture drop abstwit_01
    gen abstwit_01 = `stwit'_01`tag'
    capture drop abstwit_01_w
    winsor2 abstwit_01 if `filter', suffix(_w) cuts(1,99) 
    
    * Persistence of Disagreement
    capture drop d_stwit
    gen d_stwit = d_`stwit'
    capture drop d_stwit_w
    winsor2 d_stwit if `filter', suffix(_w) cuts(1, 99)

    capture drop SUE
    astile SUE = abs_sue_2 if `filter' & !missing(abstwit_01_w) & !missing(SCEN), by(yr qtr) nq(10)
    label variable SUE "|SUE|"

    
    local attn abstwit_01_w abstwit_295_w d_stwit_w
    local KeepVars SUE CEN SCEN
    eststo clear
    foreach y in `attn'{
      foreach x in outdegree eigenvector infc{ 

      capture drop CEN
      astile CEN = `x' if `filter' & !missing(`y'), by(yr qtr) nq(10)
      eststo: quietly reghdfe `y' SUE CEN SCEN `ctrlList' `attnList' `locbiasList' `dummyList' if `filter',   noabsorb vce(cluster gvkey rdq1) 
      
      }
    }

     noisily:    esttab, ar2 star(* 0.1 ** 0.05 *** 0.01)  keep (`KeepVars')  transform(@*100) label b(2) nonote
  }   

*** Table 7: Influencer Tests
  capture drop Replies261 
  capture drop RDirect01
  capture drop WDirect01
  capture drop InflucIndx
  gen Replies261 = imen_sp_295-imen_sp_pre  
  gen RDirect01 = dirmenas_01 - dirmenas_pre
  capture drop DO261
  capture drop d_DO
  gen DO261 = 100*amen_ppos_std_sp_295
    
    capture drop yyyyqtr
    gen yyyyqtr = yr*100+qtr
    
    
    capture drop SUE
    bysort yr qtr: egen  SUE = xtile(abs_sue_2) if `filter', nq(10)


  eststo clear
  eststo: reghdfe Replies261 RDirect01 infl SUE `ctrlList' `attnList' `localbiasList' `dummmyList' if `filter',  absorb(permno yyyyqtr) vce(cluster gvkey rdq1) 
  eststo: reghdfe DO261 RDirect01 infl SUE `ctrlList' `attnList' ``localbiasList' dummmyList' if `filter' & !missing(amen_ppos_std_sp_01),  absorb(permno yyyyqtr) vce(cluster gvkey rdq1) 
  eststo: reghdfe d_ret0 RDirect01 infl SUE `ctrlList' `attnList' ``localbiasList' dummmyList' if `filter',  noabsorb vce(cluster gvkey rdq1) 
  eststo: reghdfe d_vom_ab_pre_5 RDirect01 infl SUE `ctrlList' `attnList' ``localbiasList' dummmyList' if `filter', noabsorb vce(cluster gvkey rdq1) 

  label variable RDirect01 "ANM[0,1]"
  label variable infl "INFL[0,1]"
  label variable DO261 "DO[2, 61]"
  label variable d_ret0 "d_{RET}"
  label variable d_vom_ab_pre_5 "d_{VOL}"
  label variable SUE "|SUE|"
  noisily: esttab, keep( RDirect01 infl SUE) label ar2 replace star(* 0.1 ** 0.05 *** 0.01) stats(N, fmt(%9.0fc))

*** Table 8: Google SVI;

  local s abs_sue_2_rknew

  eststo clear

  label variable logabsvi01_v4_t2_crrted "ASV[0 1]"
  label variable logabsvi295_v4_t2_crrted "ASV[2 61]"
  label variable d_logabsvi_v4_t2 "d_{ASV}"

    foreach y in logabsvi01_v4_t2_crrted logabsvi295_v4_t2_crrted d_logabsvi_v4_t2{

      capture drop SUE
      bysort yr qtr: egen SUE = xtile(abs_sue_2) if `filter' & !missing(`y') , nq(10)
      label variable SUE "|SUE|"

      foreach x in outdegree eigenvector infc{
      
        capture drop CEN
        bysort yr qtr: egen CEN = xtile(`x') if `filter' & !missing(`y') , nq(10)
    
        local KeepVars SUE CEN

        eststo: quietly reghdfe `y' SUE CEN  `ctrlList' `attnList' `dummyList' `locbiasList'  if `filter',  noabsorb vce(cluster gvkey rdq1)  
    
      }
       
    }
  noisily:    esttab, ar2 star(* 0.1 ** 0.05 *** 0.01)  keep (`KeepVars') stats(N, fmt(%9.0fc)) transform(@*100) label b(3) compress

*** Table 10: StockTwit Centrality

  local window past_3m

  /* Return */
  local ret abcret01_dgtw abcret295_dgtw_r
  capture drop SUE
  bysort yr qtr: egen SUE = xtile(sue_2) if `filter' & !missing(total_msg_`window'), nq(10)
  capture drop ABS_SUE
  bysort yr qtr: egen ABS_SUE = xtile(abs_sue_2) if `filter' & !missing(total_msg_`window'), nq(10)

  foreach x in total_msg_`window'{ 
      capture drop `x'_rkstwit
      astile `x'_rkstwit = `x' if `filter' & !missing(total_msg_`window'), nq(10)
  }

  foreach y in total_msg_`window'{
      capture drop tmp
      gen tmp = `y'
      replace tmp = 0 if yr >= 2009 & yr <= 2013 & missing(`y')
      replace tmp = . if yr <= 2008
      capture drop `y'_rkmedia
      astile `y'_rkmedia = tmp if yr>= 2009 & yr <= 2013, by(yr qtr) nq(10)
  }
  label variable total_msg_`window'_rkmedia "SCEN"

  local s sue_2_rknew
  local cxn total_msg_`window'_rkmedia 

  foreach y in `ret'{
    eststo clear
    foreach x in `cxn'{
    capture drop SCEN
    gen SCEN = `x'
      capture drop EC
      gen EC = eigenvector_rknew
      capture drop DC
      gen DC = outdegree_rknew
      capture drop IC
      gen IC = infc_rknew
      eststo: quietly reghdfe `y' `s' c.SCEN#c.`s' c.SCEN `ctrlInteract' `attnInteract' `dummyInteract' `locbiasInteract'  if `filter', noabsorb vce(cluster gvkey rdq1)  
      eststo: quietly reghdfe `y' `s' c.SCEN#c.`s' c.SCEN  c.DC#c.`s' c.DC `ctrlInteract' `attnInteract' `dummyInteract' `locbiasInteract'  if `filter', noabsorb vce(cluster gvkey rdq1)  
      eststo: quietly reghdfe `y' `s' c.SCEN#c.`s' c.SCEN  c.EC#c.`s' c.EC `ctrlInteract' `attnInteract' `dummyInteract' `locbiasInteract'  if `filter', noabsorb vce(cluster gvkey rdq1)  
      eststo: quietly reghdfe `y' `s' c.SCEN#c.`s' c.SCEN  c.IC#c.`s' c.IC `ctrlInteract' `attnInteract' `dummyInteract' `locbiasInteract'  if `filter', noabsorb vce(cluster gvkey rdq1)  
    }
    local KeepVars c.SCEN#c.`s' SCEN `s' c.DC#c.`s' c.EC#c.`s' c.IC#c.`s'	    
    noisily:    esttab, ar2  star(* 0.1 ** 0.05 *** 0.01)  keep (`KeepVars') compress nonote label
  }
                                                               
  /* Volume tests */
  capture drop vom_ab01_pre_rw 
  capture drop vom_ab95_pre_rw 
  winsor2 vom_ab01_pre_r if `filter', suffix(w) cuts(1, 99)
  winsor2 vom_ab95_pre_r if `filter', suffix(w) cuts(1, 99)	
  label variable vom_ab01_pre_rw "VOL[0 1]"
  label variable vom_ab95_pre_rw "VOL[2 95]"
      
                                                                   
  foreach y in vom_ab01_pre_rw vom_ab95_pre_rw {  
    eststo clear
    label variable vom_ab01_pre_rw "VOL[0 1]"
    label variable vom_ab95_pre_rw "VOL[2 95]"
    label variable d_vom_ab_pre_5 "d_{VOL}"
    
  foreach x in `cxn'{
    local s abs_sue_2_rknew
    capture drop SCEN
    gen SCEN=`x'
    
    local KeepVars  `s' SCEN DC EC IC
    
    eststo: quietly reghdfe `y'  `s' SCEN `ctrlList' `attnList'  `dummyList' `locbiasList' if `filter', noabsorb  vce(cluster gvkey rdq1) 
    eststo: quietly reghdfe `y'  `s' SCEN DC `ctrlList' `attnList'  `dummyList' `locbiasList' if `filter', noabsorb  vce(cluster gvkey rdq1) 
    eststo: quietly reghdfe `y'  `s' SCEN EC `ctrlList' `attnList'  `dummyList' `locbiasList' if `filter', noabsorb  vce(cluster gvkey rdq1) 
    eststo: quietly reghdfe `y'  `s' SCEN IC `ctrlList' `attnList'  `dummyList' `locbiasList' if `filter', noabsorb  vce(cluster gvkey rdq1) 
    }
    noisily:    esttab, star(* 0.1 ** 0.05 *** 0.01)  keep (`KeepVars') compress nonote transform(@*100) ar2 label b(3) 
  }






gvkey
permno 
fyearq
fqtr
rdq1
ba_state
end_date
beg_date
type
no_hqadd_ctychg
county_group
exchcd
abcret01_dgtw
abcret240_dgtw_r
abcret295_dgtw_r
vom_ab01_pre_r
vom_ab95_pre_r
d_ret0
d_vom_ab_pre_5
ab_dirmenas_01
ab_dirmenas_295
ab_imen_sp_01
ab_imen_sp_295
amen_ppos_std_sp_01
amen_ppos_std_sp_295
amen_ppos_std_sp_pre
imen_ppos_std_sp_01
imen_ppos_std_sp_295
infl
logabsvi01_v4_t2_crrted
logabsvi295_v4_t2_crrted
d_logabsvi_v4_t2
total_msg_past_3m
sue_2
outdegree
eigenvector
infc
size
bm
idvol
ior
urban
ep2
evol2
nrdq
rptlag
xad
sp500
retail
pop_density
sind_p_ctex
mean_age_ct
retire_ratio_ct
median_income_ctex
mean_edu
movedin_ctex
ff10
